﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Configuration;
using System.Net.Mail;
using System.Text;
using System.Web.Configuration;

namespace Common
{
    public class Mail
    {
        #region 字段属性
        private string _host;
        public string Host
        {
            get { return _host; }
            set { _host = value; }
        }
        private int _port;
        public int Port
        {
            get { return _port; }
            set { _port = value; }
        }
        private string _smtpUsername;
        public string SmtpUsername
        {
            get { return _smtpUsername; }
            set { _smtpUsername = value; }
        }
        private string _smtpPassword;
        public string SmtpPassword
        {
            get { return _smtpPassword; }
            set { _smtpPassword = value; }
        }
        private string _from;
        public string From
        {
            get { return _from; }
            set { _from = value; }
        }
        private bool _defaultCredentials;
        public bool DefaultCredentials
        {
            get { return _defaultCredentials; }
            set { _defaultCredentials = value; }
        }
        private bool _enableSsl;
        public bool EnableSsl
        {
            get { return _enableSsl; }
            set { _enableSsl = value; }
        }
        #endregion 字段属性

        public Mail()
        {
            this._enableSsl = false;

            //从web.config读取邮件配置
            System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("~/");
            MailSettingsSectionGroup mailSettings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
            this._smtpUsername = mailSettings.Smtp.Network.UserName;
            this._smtpPassword = mailSettings.Smtp.Network.Password;
            this._from = mailSettings.Smtp.From;
            this._defaultCredentials = mailSettings.Smtp.Network.DefaultCredentials;
            this._host = mailSettings.Smtp.Network.Host;
            this._port = mailSettings.Smtp.Network.Port;
        }

        public bool Send(string toMail, string subject, string body)
        {
            try
            {
                Send(toMail, subject, body, (string[])null);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public string Send(string to, string subject, string body, string[] bcc)
        {
            var Result = "";
            // Create mail message
            MailMessage message = new MailMessage(this.From, to, subject, body);
            message.BodyEncoding = Encoding.GetEncoding(936);
            message.IsBodyHtml = true;
            ////抄送
            //if (cc != null && cc.Length > 0)
            //{
            //    foreach (string ccAddress in cc)
            //    {
            //        message.CC.Add(new MailAddress(ccAddress));
            //    }
            //}
            //暗送
            if (bcc != null && bcc.Length > 0)
            {
                foreach (string bccAddress in bcc)
                {
                    message.Bcc.Add(new MailAddress(bccAddress));
                }
            }
            // Send email
            SmtpClient client = new SmtpClient(this._host, this._port);
            if (!String.IsNullOrEmpty(this._smtpUsername) && !String.IsNullOrEmpty(this._smtpPassword))
            {
                client.Credentials = new NetworkCredential(this._smtpUsername, this._smtpPassword);
            }
            client.EnableSsl = this.EnableSsl;
            try
            {
            client.Send(message);
               Result = "邮件已经发送成功";
            }
            catch (Exception err)
            {

                Result =err.Message;
            }

            return Result;
        }
    }
}
